package tianrun.ziguan.api.calculate.config.service.influxdb.impl;

import org.influxdb.InfluxDBMapperException;
import org.influxdb.dto.Query;
import org.springframework.stereotype.Service;
import tianrun.ziguan.api.calculate.config.config.influxdb.DynamicInfluxDbProperties;
import tianrun.ziguan.api.calculate.config.config.influxdb.InfluxDbProperties;
import tianrun.ziguan.api.calculate.config.service.influxdb.IInfluxDbService;
import tianrun.ziguan.api.calculate.config.utils.DecimalFormatUtil;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class IInfluxDbServiceImpl implements IInfluxDbService {

    private final Map<String, QueryInfluxDb> influxDB;

    public IInfluxDbServiceImpl(DynamicInfluxDbProperties properties) {
        Map<String, InfluxDbProperties> datasource = properties.getDatasource();
        influxDB = new ConcurrentHashMap<>();
        for (Map.Entry<String, InfluxDbProperties> entry : datasource.entrySet()) {
            String sourceName = entry.getKey();
            InfluxDbProperties influxDbProperties = entry.getValue();
            QueryInfluxDb value = new QueryInfluxDb(influxDbProperties);
            influxDB.put(sourceName, value);
        }
    }

    @Override
    public <T> List<T> query(String sourceName, Query query, Class<T> clazz) {
        InfluxDBMapperException ex = null;
        for (int i = 1; i < 4; i++) {
            try {
                return influxDB.get(sourceName).query(query, clazz);
            } catch (InfluxDBMapperException e) {
                System.out.println(i + 1 < 4 ? ("influx查询超时,即将再次重试第" + i + "次") : ("influx查询超时,不再重试"));
                try {Thread.sleep(500);} catch (InterruptedException exx) {}
                ex=e;
            }
        }
        throw ex;
    }

    @Override
    public <T> List<T> queryTodayData(String sourceName, String sql, Class<T> clazz) {
        return influxDB.get(sourceName).queryTodayData(sql, clazz);
    }

    @Override
    public boolean insert(String sourceName, String measurement, String label, Long time, Double value) {
        //最长保留8位,防止科学记数法
        value= DecimalFormatUtil.parseDoubleByEight(value);
        InsertValue timeValue = new InsertValue().setTime(time).setLabel(label).setValue(value).setAssetNum(measurement);
        return influxDB.get(sourceName).insert(timeValue);
    }

    @Override
    public boolean insertBatch(String sourceName,String dbName, Collection<InsertValue> values) {
        return influxDB.get(sourceName).insertBatch(dbName, values);
    }

    @Override
    public boolean deleteLabelByDate(String sourceName,String dbName,String assetNum,String label, long startDate, long endDate) {
        String sql=String.format("DELETE FROM \"%s\" WHERE  label='%s' and time >=%sms and time <=%sms;",
                assetNum,
                label,
                startDate,
                endDate);
        return influxDB.get(sourceName).delete(sql,dbName);
    }
}
